<?php

/**
 * crpVideo
 *
 * @copyright (c) 2007,2009 Daniele Conca
 * @link http://code.zikula.org/crpvideo Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpVideo
 */

/**
 * crpVideoUI
 */
class crpVideoUI
{

	function crpVideoUI()
	{
		// none
	}

	/**
	 * Draw modify configuration form
	 *
	 * @param array $item element values
	 * @param int $mainCat module's root category
	 * @param array $modvars module's variables
	 *
	 * @return string html
	 */
	function modifyConfig($modvars= array (), $gdArray= array (), $mainCat= null)
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpVideo', false);

		$pnRender->assign($modvars);
		$pnRender->assign('gd_version', $this->gd_version($gdArray['GD Version']));
		$pnRender->assign('mainCategory', $mainCat);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpvideo_admin_modifyconfig.htm');
	}

	function gd_version($fullstring= null)
	{
		$cache_gd_version= array ();

		if (eregi('bundled \((.+)\)$', $fullstring, $matches))
		{
			$cache_gd_version['string']= $fullstring; // e.g. "bundled (2.0.15 compatible)"
			$cache_gd_version['value']= (float) $matches[1]; // e.g. "2.0" (not "bundled (2.0.15 compatible)")
		}
		else
		{
			$cache_gd_version['string']= $fullstring; // e.g. "1.6.2 or higher"
			$cache_gd_version['value']= (float) substr($fullstring, 0, 3); // e.g. "1.6" (not "1.6.2 or higher")
		}

		return $cache_gd_version;
	}

	/**
	 * User display function
	 *
	 * @param int videoid identifier
	 * @param string title video name
	 * @param array video data
	 * @param array item data
	 *
	 * @return string output
	 */
	function userDisplay($videoid= null, $title= null, $item= array ())
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpVideo');

		// Regardless of caching, we need to increment the read count and set the cache ID
		if (isset ($videoid) && is_numeric($videoid))
		{
			$pnRender->cache_id= $videoid;
			$incrementresult= pnModAPIFunc('crpVideo', 'user', 'incrementreadcount', array (
				'videoid' => $videoid
			));
		}
		else
		{
			$pnRender->cache_id= $title;
			$incrementresult= pnModAPIFunc('crpVideo', 'user', 'incrementreadcount', array (
				'title' => $title
			));
		}
		if ($incrementresult == false)
		{
			return LogUtil :: registerError(_NOSUCHITEM, 404);
		}

		// load the categories system
		if (pnModGetVar('crpVideo', 'enablecategorization'))
		{
			Loader :: loadClass('CategoryUtil');
			$cat= CategoryUtil :: getCategoryByID($item['__CATEGORIES__']['Main']['id']);
			$cats= CategoryUtil :: getCategoriesByParentID($cat['id']);
			$pnRender->assign('categories', $cats);
			$pnRender->assign('category', $cat);
		}

		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign(pnModGetVar('crpVideo'));

		// determine which template to render this page with
		// A specific template may exist for this page (based on video id)
		if ($pnRender->template_exists("crpvideo_user_display_$videoid"))
		{
			$template= "crpvideo_user_display_$videoid";
		}
		elseif ($pnRender->template_exists("crpvideo_user_display_$title"))
		{
			$template= "crpvideo_user_display_$title";
		}
		else
		{
			$template= 'crpvideo_user_display.htm';
		}

		// check if the contents are cached.
		if ($pnRender->is_cached($template))
		{
			return $pnRender->fetch($template);
		}

		// Assign details of the item.
		$pnRender->assign($item);

		return $pnRender->fetch($template);
	}

	/**
	 * Draw rss feed
	 *
	 * @param array $data feed values
	 * @param array $list elements
	 *
	 * @return string xml
	 */
	function drawFeed($data= array (), $list= array ())
	{
		$pnRender= pnRender :: getInstance('crpVideo', false);
		$pnRender->assign('data', $data);
		$pnRender->assign('list', $list);

		return $pnRender->fetch('crpvideo_user_getfeed.htm');
	}

	/**
	 * Draw rss feed
	 *
	 * @param array $data feed values
	 * @param array $list elements
	 *
	 * @return string xml
	 */
	function drawPlaylist($list= array ())
	{
		$pnRender= pnRender :: getInstance('crpVideo', false);
		$pnRender->assign('list', $list);

		return $pnRender->fetch('crpvideo_user_getplaylist.htm');
	}

	/**
	 * Draw podcast feed
	 *
	 * @param array $data feed values
	 * @param array $list elements
	 *
	 * @return string xml
	 */
	function drawPodcast($data= array (), $list= array (), $modvars= array ())
	{
		$pnRender= pnRender :: getInstance('crpVideo', false);
		$pnRender->assign('data', $data);
		$pnRender->assign('list', $list);
		$pnRender->assign($modvars);

		return $pnRender->fetch('feeds/crpvideo_template_podcast.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function uploadersList($rows= array (), $category= null, $mainCat= null, $rootCat= null, $cats= null, $modvars= array (), $active= null)
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpVideo');

		// add Profile define
		pnModLangLoad('Profile');

		// Assign the items to the template
		$pnRender->assign('users', $rows);
		$pnRender->assign('category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('rootCat', $rootCat);
		$pnRender->assign('categories', $cats);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		// Assign the information required to create the pager
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpVideo', 'user', 'countitems', array (
				'category' => $category,
				'active' => $active,
				'uploaders' => true
			)),
			'itemsperpage' => $modvars['itemsperpage']
		));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpvideo_user_view_uploaders.htm');
	}

	/**
	 * draw user events list
	 *
	 * @param array $rows of event's value
	 * @param int $category current category if specified
	 * @param int $mainCat module root category
	 * @param array $modvars module's variables
	 *
	 * @return string html code
	 */
	function uploadsList($videos= array (), $category= null, $mainCat= null, $rootCat= null, $cats= null, $modvars= array (), $uid= null, $active= null)
	{
		// Create output object
		$pnRender= pnRender :: getInstance('crpVideo');

		// add Profile define
		pnModLangLoad('Profile');

		// Assign the items to the template
		$pnRender->assign('videos', $videos);
		$pnRender->assign('category', $category);
		$pnRender->assign('mainCategory', $mainCat);
		$pnRender->assign('rootCat', $rootCat);
		$pnRender->assign('categories', $cats);
		$pnRender->assign('uid', $uid);
		$pnRender->assign('lang', pnUserGetLang());
		$pnRender->assign($modvars);

		// Assign the information required to create the pager
		$pnRender->assign('pager', array (
			'numitems' => pnModAPIFunc('crpVideo', 'user', 'countitems', array (
				'category' => $category,
				'active' => $active,
				'uid' => $uid
			)),
			'itemsperpage' => $modvars['itemsperpage']
		));

		// Return the output that has been generated by this function
		return $pnRender->fetch('crpvideo_user_view_uploads.htm');
	}

}
?>
